#!/usr/bin/python3
# -*- coding: utf-8 -*-
# ======================================#
# @Author   : duanqizhong
# @time     : 2021-11-6 14:02
# @file     : other_api
# @Software : PyCharm
# ======================================#
from flask import Blueprint, make_response, jsonify, request

from apps.api.common_func import env_database_info, env_host_info, env_business_var_info, env_all_business_var_info, \
    env_business_host_info, env_business_database_info, env_app_var_info, get_env_app_port
from apps.config import ERROR_RESPONSE

env_bp = Blueprint("env", __name__, url_prefix="/api/v1/env")


@env_bp.route("/database/info", methods=("GET",))
def env_database():
    try:
        args = request.args
        env = args["env"]
        infos = env_database_info(env)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


@env_bp.route("/host/info", methods=("GET",))
def env_host():
    try:
        args = request.args
        env = args["env"]
        infos = env_host_info(env)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


@env_bp.route("/var/info", methods=("GET",))
def env_var():
    try:
        args = request.args
        env = args["env"]
        infos = env_all_business_var_info(env)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


# business
@env_bp.route("/business/database/info", methods=("GET",))
def env_business_database():
    try:
        args = request.args
        env = args["env"]
        business = args["business"]
        infos = env_business_database_info(env, business)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


@env_bp.route("/business/host/info", methods=("GET",))
def env_business_host():
    try:
        args = request.args
        env = args["env"]
        business = args["business"]
        infos = env_business_host_info(env, business)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


@env_bp.route("/business/var/info", methods=("GET",))
def env_business_var():
    try:
        args = request.args
        env = args["env"]
        business = args["business"]
        infos = env_business_var_info(env, business)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


# app
@env_bp.route("/business/app/info", methods=("GET",))
def env_app():
    try:
        args = request.args
        env = args["env"]
        app = args["app"]
        infos = env_app_var_info(env, app)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))


@env_bp.route("/app/port", methods=("GET",))
def env_app_port():
    try:
        args = request.args
        env = args.get("env")
        infos = get_env_app_port(env)
        return make_response(jsonify(infos))
    except Exception as e:
        ERROR_RESPONSE["msg"] = str(e)
    return make_response(jsonify(ERROR_RESPONSE))
